package simplethread;

import java.util.Random;

import controller.main;
public class CalculSimpleThread extends Thread {
	
	private double Result;
	private double Time;
	private double dt;
	private double Sum;
	private double Drift;
	private double vSqrdt;
	private double Strike;
	private double Stock;
	private double i=0;
	private Integer j=0;
	private Integer z;
	private double Rate;
	private double Mat;
	private Integer nSteps;
	private final Integer nb_coeur = Runtime.getRuntime().availableProcessors();;
	final double blockingCoefficient = 0.9;
    final int poolSize = (int)(nb_coeur / (1 - blockingCoefficient));
    private Integer nSimulation;
	
	
	public CalculSimpleThread(String CallPutFlag,double stock,double strike,double mat,double rate,double b, double v,Integer nSteps, Integer nSimulation)
	{
		if(CallPutFlag == "chris")
		{
		this.z = 1;
		}
		else if(CallPutFlag == "patrick")
		{
		this.z = -1;
		}
		
		this.Stock=stock;
		this.Strike=strike;
		this.nSteps=nSteps;
		this.nSimulation=nSimulation;
		this.dt = mat / nSteps;
		this.Rate=rate;
		this.Mat= mat;
		this.Drift = (b -  java.lang.Math.pow(v,2) / 2) * dt;
		this.vSqrdt = v * java.lang.Math.sqrt(dt);
	}
	
	public void CalculOneThread()
	{
		Random rand = new Random() ;
		double St=0;
		
		
		for(this.i=1;i<=this.nSimulation;i++)
		{
			St = this.Strike ;
			for(this.j=1;this.j<=this.nSteps;this.j++)
			{
			
		    St = St * Math.exp(this.Drift + this.vSqrdt * rand.nextGaussian());
		 
			}
			
			this.Sum = this.Sum + Math.max(this.z*(St-this.Stock),0);
			main.GUI.SimpleProgressBar.setValue((int) i);
		}
		
		 this.setResult(java.lang.Math.exp((-this.Rate) * this.Mat)*(this.Sum/this.nSimulation));
	}

	public double getResult() {
		return Result;
	}

	private void setResult(double result) {
		Result = result;
	}

	public double getTime() {
		return Time;
	}	
}

